package com.ruoyi.web.common.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.format.TimeUtils;
import com.ruoyi.iot.domain.IotSensorMsg;

import java.util.*;
import java.util.stream.Collectors;

public class IOTSensorMsgUtil {

    //登录
    private static final String API_URL_LOGIN = "https://stll.hubeishuiyi.cn:9096/api/manager/login";

    //白蚁数据
    private static final String API_URL_Amnitro = "https://stll.hubeishuiyi.cn:9096/api/device/search-message?station_id=2226&rtu_id=&start_time=&end_time=&page=1&pageSize=13&send_type=52";

    //遥测终端-（测试设备编号非实际使用）
    private static final List<String>  YCZDDevices = Arrays.asList(
            "4208213357",
            "4208214357",
            "4208215357",
            "4208216357",
            "4208212157",
            "4208210157",
            "4208211157",

            "4208214157",
            "4208215157",
            "4208216157",//副坝mcu
            "4208217157",
            "4208218157",
            "4208219157"
    );
    //MCU终端-（测试设备编号非实际使用）
    private static final List<String>  MCUDevices = Arrays.asList(
            "4208214157",
            "4208215157",
            "4208216157",
            "4208217157",
            "4208218157",
            "4208219157",
            "4208211157"
    );



    public static JSONObject login(String url, String username, String password)
    {
        // 创建 HttpRequest 对象
        HttpRequest request = HttpUtil.createPost(url);
        // 设置请求体参数为 form 格式
        request.form("user_name", username)
                .form("password", password);
        // 发送请求并获取响应
        String response = request.execute().body();
        // 将响应字符串解析为 JSONObject
        JSONObject jsonObject = JSONUtil.parseObj(response);
        return jsonObject;
    }

    public static JSONObject fetchDataWithToken(String url,String token)
    {
        // 创建 HttpRequest 对象
        HttpRequest request = HttpUtil.createGet(url);

        // 添加请求头，包含 token
        // 添加请求头，包含 token
        request.header("token", token);
//        request.header("Cookie", "_csrf=" + cookie);

        // 发送请求并获取响应
        String response = request.execute().body();

        // 将响应字符串解析为 JSONObject
        JSONObject jsonObject = JSONUtil.parseObj(response);

        return jsonObject;
    }


    //没有将设备的采集时间赋值过去
    public static List<IotSensorMsg> convertUnits() {
        String dada = login(API_URL_LOGIN, "jingshan_admin", "@VJR;.f.").get("data").toString();
        JSONObject jsonObject = JSONUtil.parseObj(dada);
        String token = jsonObject.get("token").toString();

        JSONObject data = fetchDataWithToken(API_URL_Amnitro,token);
        JSONObject map = data.getByPath("data", JSONObject.class);
        List<JSONObject> list = map.getByPath("result", List.class);
        // 使用 Stream 过滤 + 转换
        List<IotSensorMsg> result = list.stream()
                .filter(item -> YCZDDevices.contains(item.getStr("rtu_id"))) // 排除 termiteDeviceList 中的数据
                .map(item -> {
                    IotSensorMsg termite = new IotSensorMsg();
                    String remoteId = item.getStr("rtu_id");
                    termite.setRemoteId(remoteId);
                    termite.setFuncCode("34");
                    termite.setMsg(item.getStr("request_log"));
                    String reportTime = TimeUtils.formatTimestamp(item.getStr("created_at"));
                    termite.setSendTime(reportTime);
                    termite.setReportTime(reportTime);
                    termite.setType("遥测终端");
                    if (MCUDevices.contains(remoteId)){
                        termite.setType("MCU终端");
                    }

                    return termite;
                })
                .collect(Collectors.toList());


        return result;
    }


    public static void main(String[] args) {
        JSONObject login = login(API_URL_LOGIN, "jingshan_admin", "@VJR;.f.");
        System.out.println(login);
        List<IotSensorMsg> termiteList = convertUnits();
//        Map<String, Object> stringMapMap = convertParams(termiteList.get(0));
//        System.out.println(stringMapMap);
    }
}
